﻿using BaseEpub;
using Domain.EbookModel.Core;
using Domain.EbookModel.DTO;
using Domain.EbookModel.Service.Interface;
using Domain.EbookStyleModel.Service.Interface;
using Domain.Tools;
using Domain.UserModel.Service.Interface;
using EntAppFrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
    public class Default1Controller : Controller
    {
        //
        // GET: /Default1/
        public ActionResult Index()
        {

            try
            {
                DomainTest.Remoting();
                string opfPath = @"E:\MyCode\NetCode\AngleSharpDemo\有机化学样本3\有机化学样本3\OEBPS\content.opf";
                //EbookInfo ebookInfo = new NdPressOpf().GetEbookInfo(opfPath);
                EbookInfo ebookInfo = new NdPressOpf().GetEbookInfo(opfPath);

                EpubToEbookDto ebInfoDto = new EpubToEbookDto();
                ebInfoDto.Author = ebookInfo.Author;
                ebInfoDto.Cover = ebookInfo.Cover;
                ebInfoDto.Isbn = ebookInfo.Isbn;
                ebInfoDto.Name = ebookInfo.Name;

                INcxBase ncx = new NdPressNcx();
                string ncxPath = @"E:\MyCode\NetCode\AngleSharpDemo\有机化学样本3\有机化学样本3\OEBPS\toc.ncx";

               
                IList<SectionBaseInfo> secList = ncx.GetSecList(ncxPath);


                IList<SectionBase> listSecs = new List<SectionBase>();
                IList<Page> listPages = new List<Page>();

                Dictionary<IList<SectionBaseInfo>, IList<SectionBase>> dic = new Dictionary<IList<SectionBaseInfo>, IList<SectionBase>>();
                IList<SectionBase> secLi = new List<SectionBase>();
                dic.Add(secList, secLi);
                //Dictionary<Page, PageContentInfo> pdic = new Dictionary<Page, PageContentInfo>();
                Dictionary<SectionBaseInfo, SectionBase> dicsec = new Dictionary<SectionBaseInfo, SectionBase>();
                Dictionary<SectionBase, SectionBaseInfo> dicsec2 = new Dictionary<SectionBase, SectionBaseInfo>();

                IList<Domain.EbookModel.DTO.EpubToSectionBaseDto> epubSecList = new List<Domain.EbookModel.DTO.EpubToSectionBaseDto>();
                foreach (var item in secList)
                {
                    Domain.EbookModel.DTO.EpubToSectionBaseDto eSecDto = new Domain.EbookModel.DTO.EpubToSectionBaseDto();
                    eSecDto.FinalParentId = item.FinalParentId;
                    eSecDto.Grade = item.Grade;
                    eSecDto.Name = item.Name;
                    eSecDto.OrderBy = item.OrderBy;
                    eSecDto.ParentId = item.ParentId;
                    eSecDto.SecId = item.SecId;
                    eSecDto.Sort = item.Sort;

                    Domain.EbookModel.DTO.EpubToPageDto ePageDto=new Domain.EbookModel.DTO.EpubToPageDto();
                    ePageDto.Content=item.PageContentInfo.Content;
                    ePageDto.PageNum=item.PageContentInfo.PageNum;
                    eSecDto.EpubToPageDto = ePageDto;

                    epubSecList.Add(eSecDto);
                }

                Ebook result = EntAppFrameWorkContext.DomainModelService.ExtenedService<IEbookManage>().EpubImport2(epubSecList, 68719481330, ebInfoDto, 68719481504, 68719481329);
                if (result!=null)
                {
                    string cssPath = @"E:\MyCode\NetCode\AngleSharpDemo\有机化学样本3\有机化学样本3\OEBPS\Styles\style.css";
                    
                    //导入样式
                    var styleContent = System.IO.File.ReadAllText(cssPath, System.Text.Encoding.UTF8);
                    bool result2 = EntAppFrameWorkContext.DomainModelService.ExtenedService<IStyle>().CreateEbookTextStyle(result.Name + "epub导入样式", result.Name, styleContent, result.Key);
                }

               // Ebook eb = EntAppFrameWorkContext.DomainModelService.ExtenedService<IEbook>().GetEbookById(68719481510);
               //Domain.UserModel.Core.User u = EntAppFrameWorkContext.DomainModelService.ExtenedService<IUser>().GetUser(68719481330);

                //foreach (var item in secList)
                //{

                //    SectionBase sec = new SectionBase();
                //    dicsec.Add(item, sec);
                //    //dicsec2.Add(sec, item);
                //    sec.Ebook = eb;
                //    sec.CreateUser = u;

                //    PageContentInfo pp = new PageContentInfo();
                //    pp.Content = item.PageContentInfo.Content;
                //    pp.PageNum = item.PageContentInfo.PageNum;

                //    Page p = new Page();
                //    p.Content = pp.Content;
                //    p.PageNumber = pp.PageNum;
                //    p.Section = sec;
                //    p.Ebook = sec.Ebook;
                //    p.CreateUser = sec.CreateUser;
                //    p.IsValid = true;
                //    p.LastUpdateTime = DateTime.Now;
                //    p.CreateTime = DateTime.Now;


                //    //dic.Add(item,sec);
                //    //pdic.Add(p, pp);
                //    sec.Cover = string.Empty;
                //    sec.CreateTime = DateTime.Now;

                //    if (item.ParentId == 0)
                //    {
                //        sec.Parent = null;
                //    }
                //    else
                //    {
                //        SectionBaseInfo sbInfo= secList.Where(m => m.ParentId == item.ParentId).FirstOrDefault();
                //        SectionBase parent = dicsec[sbInfo];
                //        sec.Parent = parent;//dicsec[secList.Where(m => m.ParentId == item.ParentId).First()].Parent;
                //    }

                //    if (item.ParentId == 0)
                //    {
                //        sec.FinalParent = sec;
                //    }
                //    else
                //    {
                //        sec.FinalParent = sec.Parent.FinalParent;
                //    }

                //    //sec.Grade
                //    sec.IndexNumber = string.Empty;
                //    if (item.Grade == 1)
                //    {
                //        sec.IsShowChapter = true;
                //    }
                //    else
                //    {
                //        sec.IsShowChapter = false;
                //    }
                //    sec.IsValid = true;
                //    sec.LastUpdateTime = DateTime.Now;
                //    sec.Name = item.Name;
                //    sec.OrderBy = item.OrderBy;
                //    sec.OriginalEbookNum = string.Empty;

                //    sec.PositionPage = p;
                    
                //    sec.PrefixName = string.Empty;
                //    sec.Sort = item.Sort;
                //    listSecs.Add(sec);
                //    listPages.Add(p);
                //}

                
            }
            catch (Exception ex)
            {

            }

            return View();
        }
	}
}